//
//  ZYWelcomeViewController.swift
//  WeiBo
//
//  Created by zhangyu on 16/9/23.
//  Copyright © 2016年 zhangyu. All rights reserved.
//

import UIKit
import SDWebImage

class ZYWelcomeViewController: UIViewController {
    //  MARK: --懒加载
    //  背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //  头像
    private lazy var userImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = ZYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large{
            view.sd_setImage(with: URL(string: imageUrl),placeholderImage:UIImage(named: "avatar_default_big"))
        }
        view.layer.cornerRadius = view.size.width / 2
        view.layer.masksToBounds = true
        
        return view
    }()
    //  文本信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 13)
//        label.text = "欢迎回来"
        if let name = ZYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name {
            label.text = "欢迎回来, \(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        return label
        
    }()
    
    
    override func loadView() {
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    //  添加控件设置约束
    private func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        userImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false

        view.addConstraint(NSLayoutConstraint(item: userImageView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: userImageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 200))

        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: userImageView, attribute: .centerX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: userImageView, attribute: .bottom, multiplier: 1, constant: 10))
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        view.addConstraint(NSLayoutConstraint(item: userImageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100))
        //  执行约束动画
        //  usingSpringWithDamping 阻尼 0 - 1, 阻尼越大弹簧效果越小
        //  initialSpringVelocity 初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.messageLabel.alpha = 1
                }, completion: { (_) in
                    //  完成切换界面到首页
                    NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
            })
        }
    }
}
